package org.carabiner.infinitest.swingui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.ToolTipManager;
import javax.swing.tree.TreeModel;

public class DefaultInfinitestMainFrame extends JFrame implements InfinitestView
{
  private static final String APP_TITLE = "Infinitest";
  private JToolBar toolbar;
  private ColorAnimator animator;
  private CustomProgressBar progressBar;
  private JTree tree;

  public DefaultInfinitestMainFrame() throws FileNotFoundException, IOException
  {
    URL iconURL = DefaultInfinitestMainFrame.class.getResource("infinitest-icon.png");
    setIconImage(new ImageIcon(iconURL).getImage());
    setAlwaysOnTop(true);
    setLayout(new BorderLayout());
    addWindowListener(new WindowBoundsManager());
    toolbar = createToolBar();
    animator = new ColorAnimator();
    progressBar = createProgressbar(animator);
    tree = createTree();
    setBounds(WindowBoundsManager.getWindowBounds());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    add(progressBar, BorderLayout.SOUTH);
    add(toolbar, BorderLayout.WEST);
    add(new JScrollPane(tree), BorderLayout.CENTER);
  }
  
  private static JToolBar createToolBar()
  {
    JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);
    return toolbar;
  }

  private static JTree createTree()
  {
    JTree resultsTree = new ResultsTree();
    resultsTree.setRootVisible(false);
    resultsTree.setEditable(false);
    resultsTree.setShowsRootHandles(true);
    resultsTree.setCellRenderer(new FailureCellRenderer());
    ToolTipManager tipManager = ToolTipManager.sharedInstance();
    tipManager.registerComponent(resultsTree);
    tipManager.setDismissDelay(10000);
    resultsTree.addMouseListener(new ClipboardCopyListener());
    return resultsTree;
  }
  
  private static CustomProgressBar createProgressbar(ColorAnimator animator)
  {
    CustomProgressBar progressBar = new CustomProgressBar(animator);
    progressBar.setForeground(Color.YELLOW);
    return progressBar;
  }

  private JProgressBar getProgressBar()
  {
    return progressBar;
  }
  
  private JToolBar getToolbar()
  {
    return toolbar;
  }
  
  private JTree getTree()
  {
    return tree;
  }
  
  public void setAngerBasedOnTime(long timeSinceGreen)
  {
    animator.setAngerBasedOnTime(timeSinceGreen);
  }

  public int getAngerLevel()
  {
    return animator.getAngerLevel();
  }

  public void setAngerLevel(int anger)
  {
    animator.setAngerLevel(anger);
  }

  public void setColor(Color color)
  {
    getProgressBar().setForeground(color);
  }

  public void setProgress(int progress)
  {
    getProgressBar().setValue(progress);
  }

  public void setMaximumProgress(int maxProgress)
  {
    getProgressBar().setMaximum(maxProgress);
  }

  public int getMaximumProgress()
  {
    return getProgressBar().getMaximum();
  }

  public int getProgress()
  {
    return getProgressBar().getValue();
  }

  public void setCurrentTest(String testName)
  {
    progressBar.setCurrentTest(testName);
  }

  public void setCycleTime(String timeStamp)
  {
    setTitle(APP_TITLE + " - Cycle Time: " + timeStamp);
  }

  public void setStatusName(String status)
  {
    progressBar.setStatusName(status);
  }

  public void addAction(Action action)
  {
    getToolbar().add(action);
  }

  public void setResultsModel(TreeModel results)
  {
    getTree().setModel(results);
    TreeModelExpansionListener.watchTree(getTree());
  }
}
